const common = require("./webpack.common");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const { merge } = require("webpack-merge");
const path = require("path");
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");
const TerserPlugin = require("terser-webpack-plugin");

module.exports = merge(common, {
  mode: "production",
  // 去除 sourceMap
  devtool: "none",
  output: {
    filename: "[name].[chunkHash:7].js",
  },
  optimization: {
    usedExports: true,
    concatenateModules: true,
    // 代码分包
    splitChunks: {
      chunks: "all",
    },
    minimize: true,
    minimizer: [
      // css压缩
      new OptimizeCSSAssetsPlugin({
        cssProcessorOptions: {
          // 移除注释
          discardComments: { remove: true },
        },
      }),
      new TerserPlugin({
        // 开启多线程
        parallel: true,
        sourceMap: false,
        terserOptions: {
          warnings: false,
          compress: {
            // 去除未使用的
            unused: true,
            // 去除debugger
            drop_debugger: true,
            // 去除console
            drop_console: true,
          },
          output: {
            // 去除注释
            comments: false,
          },
        },
      }),
    ],
  },
  plugins: [
    new CleanWebpackPlugin(),
    new CopyWebpackPlugin({
      patterns: [
        {
          from: path.join(__dirname, "/public"),
          to: path.join(__dirname, "/dist/public"),
        },
      ],
    }),
  ],
});
